﻿using DataProcessor.EF;
using Hjt212Parser;
using log4net;
using System;
using System.Linq;


namespace DataProcessor.Process
{
    public class ProcessorMonitor //: IProcessor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ProcessorMonitor));

        //实时数据处理
        public static void Process(string content)
        {
            try
            {
                using (var contextTarget = new PlatformVocContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn))
                    {
                        return;
                    }

                    var info = Util.GetInfoFromMn(mn);
                    if (info == null)
                    {
                        Log.Warn("MN：" + mn + " 找不到对应企业！");
                        return;
                    }
                    //data_realtime_monitor_main masterMonitor = null;
                    //   contextTarget.data_realtime_monitor_main.Where(t => t.ps_code.Equals(info.ps_code))
                    //       .FirstOrDefault(t => t.pi_code.Equals(info.port_code));
                    //if (masterMonitor == null)
                    //{
                    var masterMonitor = new data_realtime_monitor_main
                    {
                        ps_code = info.ps_code,
                        pi_code = info.port_code
                    };
                    // }
                    masterMonitor.pi_state = "正常";
                    masterMonitor.online_state = "在线";
                    masterMonitor.online_state_time = Parser.GetDateTimeByKey("DataTime", content);

                    var pollutantCodes = Parser.GetPollutantCodes(content);
                    if (pollutantCodes != null && pollutantCodes.Count > 0)
                    {
                        masterMonitor.data_time = Parser.GetDateTimeByKey("DataTime", content);
                    }
                    var factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("甲烷"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data1 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data1 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                        Log.Info("甲烷:" + masterMonitor.data1);
                    }
                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("非甲烷总烃"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data2 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data2 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                        Log.Info("非甲烷总烃:" + masterMonitor.data2);
                    }
                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("总烃"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data3 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data3 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                        Log.Info("总烃:" + masterMonitor.data3);
                    }
                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("苯"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data4 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data4 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                    }
                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("甲苯"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data5 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data5 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                    }
                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("乙苯"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data6 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data6 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                    }
                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("间二甲苯"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data7 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data7 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                    }
                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("对二甲苯"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data8 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data8 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                    }

                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("邻二甲苯"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data9 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data9 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                    }
                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("烟气流速"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data10 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data10 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                    }
                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("烟气温度"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data12 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data12 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                    }

                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("烟气压力"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data13 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data13 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                    }

                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("烟气湿度"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data14 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data14 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                    }
                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("烟道截面积"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data15 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data15 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                    }
                    factor = contextTarget.sc_pollutant_factor.FirstOrDefault(t => t.name.Equals("进样压力"));
                    if (factor != null)
                    {
                        if (!string.IsNullOrWhiteSpace(factor.code) && pollutantCodes.Contains(factor.code))
                        {
                            masterMonitor.data16 = Parser.GetFloatByKey(factor.code + "-Rtd", content);
                        }
                        else if (!string.IsNullOrWhiteSpace(factor.old_code) && pollutantCodes.Contains(factor.old_code))
                        {
                            masterMonitor.data16 = Parser.GetFloatByKey(factor.old_code + "-Rtd", content);
                        }
                    }
                    contextTarget.data_realtime_monitor_main.Add(masterMonitor);
                    contextTarget.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
    }
}
